﻿using Accord.Video;
using Accord.Video.DirectShow;
//using Accord.Video.FFMPEG;
using Accord.Video.VFW;
using System;
using System.Windows.Forms;

namespace BoilingWater
{
    public partial class CameraForm : Form
    {
        private FilterInfoCollection videoDevicesList;
        private FilterInfoCollection videoDevices; //摄像头设备
        private VideoCaptureDevice videoSource;   //视频的来源选择

        //private VideoFileWriter writer = new Accord.Video.FFMPEG.VideoFileWriter();   //写入到视频

        AVIWriter aviWriter = new Accord.Video.VFW.AVIWriter();

        private bool is_record_video = false;  //是否开始录像
        private NewFrameEventHandler newFrameEventHandler;
        System.Timers.Timer timer_count;
        int tick_num = 0;

        bool IsWriting = false;

        public CameraForm()
        {
            InitializeComponent();
        }

        private void bt_open_Click(object sender, EventArgs e)
        {
            if (is_record_video)
            {
                bt_open.Text = "停止";
                is_record_video = false;

                this.videoSource.NewFrame -= this.newFrameEventHandler;
                videoSourcePlayer.Stop();
                videoSource.Stop();
                IsWriting = false;
            }
            else
            {
                bt_open.Text = "开始";
                is_record_video = true;

                int selected_index = cb_VideoSource.SelectedIndex;
                FilterInfo filterInfo = videoDevices[selected_index];

                this.videoSource = new VideoCaptureDevice(videoDevices[selected_index].MonikerString);
                this.videoSource.NewFrame += this.newFrameEventHandler;

                VideoCapabilities vc = this.videoSource.VideoCapabilities[0];
                int h = vc.FrameSize.Height;
                int w = vc.FrameSize.Width;
                int fr = vc.AverageFrameRate;

                aviWriter.Open("recorded at " + DateTime.Now.ToString("HH - mm - ss") + ".avi", w, h);
                IsWriting = true;

                //writer.Open("recorded at " + DateTime.Now.ToString("HH-mm-ss") + ".avi", w, h, fr, VideoCodec.Default);

                videoSource.Start();
                videoSourcePlayer.VideoSource = videoSource;
                videoSourcePlayer.Start();
            }
        }

        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            lock (aviWriter)
            {
                if (is_record_video)
                {
                    aviWriter.AddFrame(eventArgs.Frame);
                    //writer.WriteVideoFrame(eventArgs.Frame);
                }
            }
        }

        private void CameraForm_Load(object sender, EventArgs e)
        {
            videoDevicesList = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo videoDevice in videoDevicesList)
            {
                cb_VideoSource.Items.Add(videoDevice.Name);
            }
            if (cb_VideoSource.Items.Count > 0)
            {
                cb_VideoSource.SelectedIndex = 0;
            }
        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            is_record_video = false;
            if (videoSource != null)
            {
                this.videoSource.NewFrame -= this.newFrameEventHandler;
                videoSourcePlayer.Stop();
                videoSource.Stop();
            }
            if (IsWriting)
            {
                //writer.Close();
                aviWriter.Close();
                IsWriting = false;
            }
        }

    }
}
